package com.kania.freestylewallparer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;


@SuppressLint("DrawAllocation")
class TargetView extends ImageView {
	
	Bitmap btmTarget;
	int nWidth, nHeight;
	
	final static float STEP = 200;
	float mRatio = 1.0f;
	int mBaseDist;
	float mBaseRatio;
	Matrix mat;
	int nSaveXPos = 0, nSaveYPos = 0;
	int nDownXPos, nDownYPos;
	boolean bIsTouchModeChanged = false;
	int nBGColor = 0xffffffff;
	boolean bIsSetGradation = false;

	public TargetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		btmTarget = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
		nWidth = btmTarget.getWidth();
		nHeight = btmTarget.getHeight();
		mat = new Matrix();
	}
	
	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		Paint pnt = new Paint();
		
		//fill background
		canvas.drawColor(nBGColor);
		
		Log.d("KANIA_LOG", "mask filter value = " + (nWidth+nHeight)/20);
		
		if(bIsSetGradation) {
			BlurMaskFilter blur = new BlurMaskFilter(20, Blur.NORMAL);
			pnt.setMaskFilter(blur);
		}
		canvas.drawBitmap(btmTarget, mat, pnt);
		//canvas.drawBitmap(btmTarget, new Rect(0,0,nWidth,nHeight), new Rect(0,0,nWidth,nHeight), null);
		
		super.onDraw(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getPointerCount() == 1) {
			Log.d("KANIA_LOG", "[touch] 1 point touch");
			switch(event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				nDownXPos = (int)(event.getX());
				nDownYPos = (int)(event.getY());
				break;
			case MotionEvent.ACTION_MOVE:
				//when moving
				int nMoveX = (int)(event.getX()) - nDownXPos;
				int nMoveY = (int)(event.getY()) - nDownYPos;
				
				nDownXPos = (int)(event.getX());
				nDownYPos = (int)(event.getY());
				
				if(bIsTouchModeChanged) {
					bIsTouchModeChanged = false;
				} else {
					nSaveXPos += nMoveX;
					nSaveYPos += nMoveY;
				}
				
				mat.setScale(mRatio, mRatio);
				mat.postTranslate(nSaveXPos, nSaveYPos);
				invalidate();
				break;
			}
		} else if(event.getPointerCount() == 2) {
			//when scaling
			Log.d("KANIA_LOG", "[touch] 2 point touch");
			int action =  event.getAction();
			int pureaction = action & MotionEvent.ACTION_MASK;
			if(pureaction == MotionEvent.ACTION_POINTER_DOWN) {
				mBaseDist = getDistance(event);
				mBaseRatio = mRatio;
			} else if (pureaction == MotionEvent.ACTION_POINTER_UP) {
				bIsTouchModeChanged = true;
			} else {
				float delta = (getDistance(event) - mBaseDist) / STEP;
				float multi = (float)Math.pow(2,  delta);
				mRatio = Math.max(0.1f, mBaseRatio * multi);
				
				float[] values = new float[9];
				mat.getValues(values);
				//int xCenter = (int)((event.getX(0) + event.getX(1)) / 2);
				//int yCenter = (int)((event.getY(0) + event.getY(1)) / 2);
				float xDelta = (values[0] * nWidth) / 2;
				float yDelta = (values[4] * nHeight) / 2;
				
				mat.setScale(mRatio, mRatio);
				mat.postTranslate(nSaveXPos, nSaveYPos);
//				mat.postTranslate(nSaveXPos - (xDelta/2), nSaveYPos - (yDelta/2));
				//imgTargetImage.setImageMatrix(mat);
				invalidate();
			}
			
		}	
		return true;
	}
	
	int getDistance(MotionEvent event) {
		int dx = (int)(event.getX(0) - event.getX(1));
		int dy = (int)(event.getY(0) - event.getY(1));
		return (int)(Math.sqrt(dx * dx + dy * dy));
	}
	
	void setBitmap(Bitmap bitmap) {
		btmTarget = bitmap;
	}
	
	void setBGColor(int color) {
		nBGColor = color;
	}
	
	void setGradation(boolean gradation) {
		bIsSetGradation = gradation;
	}
}
